import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:better_socket/better_socket.dart';

class MessageUtils {

 static void connect() {
    //创建websocket连接
    var headers = {"origin": "ws://192.168.128.1:81"};
    BetterSocket.connentSocket("ws://192.168.128.1:81", httpHeaders: headers);
    BetterSocket.addListener(onOpen: (httpStatus, httpStatusMessage) {
      print(
          "onOpen---httpStatus:$httpStatus  httpStatusMessage:$httpStatusMessage");
      _sendMessage();
    }, onMessage: (message) {
      Uint8List binaryIntList = message;
      print(binaryIntList.lengthInBytes);
      print(binaryIntList);

      print("onMessage---message:$message");
    }, onClose: (code, reason, remote) {
      print("onClose---code:$code  reason:$reason  remote:$remote");
    }, onError: (message) {
      print("onError---message:$message");
    });
 }

 static void closeSocket() {
   BetterSocket.close();
 }

 // 向服务器发送消息
 static void _sendMessage() {
    var listString = Utf8Encoder().convert('H');
    var listInt = [1, 10, 11];
    var int2utf8 = Uint8List.fromList(listInt);

    var listall = listString+int2utf8;
    Uint8List last = Uint8List.fromList(listall);

    BetterSocket.sendByteMsg(last);
    print(last);
  }

//  // 手机状态栏弹出推送的消息
//  static void _createNotification(String title, String content) async {
//    await LocalNotifications.createNotification(
//      id: _id,
//      title: title,
//      content: content,
//      onNotificationClick: NotificationAction(
//          actionText: "some action",
//          callback: _onNotificationClick,
//          payload: "接收成功！"),
//    );
//  }

//  static _onNotificationClick(String payload) {
//    LocalNotifications.removeNotification(_id);
//    _sendMessage("消息已被阅读");
//  }
}




